﻿using BaseDal;
using Common;
using Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseBll
{
    public class UploadBll
    {
        static readonly Lazy<UploadBll> bll = new Lazy<UploadBll>(() => new UploadBll());
        public static UploadBll Instance { get; } = bll.Value;
        private UploadBll() { }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Add(FileData model)
        {
            return UploadDal.Instance.AddFile(model);
        }

        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool AddList(List<FileData> list)
        {
            return UploadDal.Instance.AddList(list);
        }

        /// <summary>
        ///  修改信息
        /// </summary>
        public bool Update(decimal RelationId, List<FileData> list)
        {
            FileData model;
            string deleteIds = "";
            string updateIds = "";
            int status = 0;
            for (int i = 0; i < list.Count; i++)
            {
                model = list[i];
                if (model.FileStatus >= 0)
                {
                    status = (int)model.FileStatus;
                }
                if (status == 1)
                {
                    if (string.IsNullOrEmpty(deleteIds))
                    {
                        deleteIds = "'" + model.ID + "'";
                    }
                    else
                    {
                        deleteIds += ",'" + model.ID + "'";
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(updateIds))
                    {
                        updateIds = "'" + model.ID + "'";
                    }
                    else
                    {
                        updateIds += ",'" + model.ID + "'";
                    }
                }

            }
            if (!string.IsNullOrEmpty(deleteIds))
            {
                UploadDal.Instance.UpdateFile(RelationId, 1, deleteIds);
            }
            if (!string.IsNullOrEmpty(updateIds))
            {
                UploadDal.Instance.UpdateFile(RelationId, 0, updateIds);
            }

            return true;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="relationId"></param>
        /// <returns></returns>
        public DataTable GetFiles(decimal relationId, string relationTable)
        {
            return UploadDal.Instance.GetFiles(relationId, relationTable);
        }

        /// <summary>
        /// 根据List relationIds查询所有文件
        /// </summary>
        /// <param name="relationIds"></param>
        /// <param name="relationTable"></param>
        /// <returns></returns>
        public DataTable GetFilesForItems(List<decimal> relationIds, string relationTable)
        {
            return UploadDal.Instance.GetFilesForItems(relationIds, relationTable);
        }

        /// <summary>
        /// 通过文件ID查询单个文件
        /// </summary>
        /// <param name="fileID"></param>
        /// <returns></returns>
        public FileData GetFileByID(string fileID)
        {
            return UploadDal.Instance.GetFileByID(fileID);
        }

        /// <summary>
        /// 通过文件ID删除文件
        /// </summary>
        /// <param name="fileID"></param>
        /// <returns></returns>
        public bool DeleteFilesByID(string fileID, FileData model)
        {
            bool res = UploadDal.Instance.DeleteFilesByID(fileID);
            if (res)
            {
                // 删除文件
                Utils.DeleteFile(model.FilePath);
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="relationIds"></param>
        /// <param name="relationTable"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool DeleteFiles(string relationIds, string relationTable, List<FileData> list)
        {
            //string ids = "";
            bool res = false;

            if (!string.IsNullOrEmpty(relationIds))
            {
                res = UploadDal.Instance.DeleteFiles(relationIds, relationTable);
            }
            if (res)
            {
                // 删除文件
                foreach (var item in list)
                {
                    Utils.DeleteFile(item.FilePath);
                }
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
